<?php
namespace Admin\Controller;
use Think\AjaxPage;
class OrderController extends BaseController {
    public function index(){
        $mod = M('order');
        $where['is_delete'] = 0;
		$order_sn = I('order_sn')?I('order_sn'):'';
		$order_sn && $where['order_sn'] = array('like','%'.$order_sn.'%');
		$store_id = I('store_id')?I('store_id'):'';
		$store_id && $where['store_id'] = $store_id;
		$order_status = is_numeric(I('order_status'))?I('order_status'):'all';
		if(is_numeric($order_status)){
			$where['order_status'] = $order_status==4?0:$order_status;
			$this->assign('order_status',$order_status);
		}
        $p = empty($_REQUEST['p']) ? 1 : $_REQUEST['p'];
        $size = 20;
        $count = $mod->where($where)->count();
        $pager = new \Think\Page($count, $size);
        $page = $pager->show();
		
        $order_list = $mod->where($where)->order('add_time desc')->page("$p,$size")->select();
		
		$all_id = M('goods')->group('store_id')->getField('store_id',true);
		$user_list = M('users')->where(array('id'=>array('in',implode(',',$all_id))))->field('id,realname')->select();
		$this->assign('user_list', $user_list);
		
        $this->assign('order_list',$order_list);
        $this->assign('page', $page);
        $this->display();
    }
    public function detail(){
        $id = I('id')?I('id'):'';
        if($id){
            $mod = M('order');
            $info = $mod->where(array('id'=>$id))->find();
            $this->assign('order',$info);
			$orderGoods = M('goods')->where(array('id'=>$info['goods_id']))->select();
			$orderGoods[0]['goods_number'] = $info['goods_number'];
			foreach((array)$orderGoods as $k=>$v){
				$store_name = M('users')->where(array('id'=>$v['store_id']))->getField('realname');
				$orderGoods[$k]['store_name'] = $store_name?$store_name:'商品自营';
			}
			$action_log = M('order_log')->where(array('order_id'=>$id))->order('id desc')->select();
			foreach((array)$action_log as $k=>$v){
				if($v['user_id']){
					$name = M('admin')->where(array('id'=>$v['user_id']))->getField('user_name');
				}else{
					$name = M('users')->where(array('id'=>$info['user_id']))->getField('realname');
				}
				$action_log[$k]['username'] = $name;
			}
			$this->assign('action_log',$action_log);
			$this->assign('orderGoods',$orderGoods);
            $this->display();
        }
    }
    //发货
    public function deliveryInfo(){
        $mod = M('order');
        $id = I('order_id')?I('order_id'):'';
        $admin_id = $_SESSION['admin_id'];
        $info = $mod->where(array('id'=>$id))->field('order_status')->find();
        if($info['order_status']>0){
            exit(json_encode(array('code'=>100,'msg'=>'该订单已发货')));
        }
        $result = $mod->where(array('id'=>$id))->save(array('order_status'=>1,'send_time'=>time()));
        if($result){
            $log['user_id'] = $_SESSION['admin_id'];
			$log['order_id'] = $id;
			$log['status'] = 1;
			$log['resume'] = I('resume')?I('resume'):'确认发货';
			$log['add_time'] = time();
			M('order_log')->add($log);
            exit(json_encode(array('code'=>200,'msg'=>'发货成功')));
        }
    }
	public function order_cancle(){
		$mod = M('order');
        $id = I('order_id')?I('order_id'):'';
		$result = $mod->where(array('id'=>$id))->save(array('is_delete'=>1));
		if($result){
            $log['user_id'] = $_SESSION['admin_id'];
			$log['order_id'] = $id;
			$log['status'] = 1;
			$log['resume'] = '删除订单';
			$log['add_time'] = time();
			M('order_log')->add($log);
			exit(json_encode(array('code'=>200,'msg'=>'删除成功')));
        }
	}
}
